package com.leo;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.media.AudioSpectrumListener;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MediaPlayerDemo2 extends Application {

    private int index = 0;
    private List<Media> list = new ArrayList<>();
    private Slider sound;
    private Slider process;
    private MediaPlayer mp;
    private Button play;
    private volatile boolean playing = false;
    private volatile boolean pausing = false;
    private volatile boolean mouse = false;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        URL url1 = this.getClass().getClassLoader().getResource("bound/1.mp3");
        URL url2 = this.getClass().getClassLoader().getResource("bound/2.mp3");
        URL url3 = this.getClass().getClassLoader().getResource("bound/3.mp3");

        list.add(new Media(url1.toExternalForm()));
        list.add(new Media(url2.toExternalForm()));
        list.add(new Media(url3.toExternalForm()));

        HBox hBox = new HBox(30);
        hBox.setAlignment(Pos.CENTER);

        play = new Button("播放");
        Button stop = new Button("停止");
        Button pre = new Button("上一首");
        Button next = new Button("下一首");

        sound = new Slider(0, 1, 0.2);
        sound.setPrefWidth(100);

        process = new Slider(0, 1, 0);
        process.setPrefWidth(400);

        hBox.getChildren().addAll(pre, play, next, stop, process, sound);

        AnchorPane root = new AnchorPane();
        root.getChildren().addAll(hBox);
        AnchorPane.setTopAnchor(hBox, 20d);
        AnchorPane.setLeftAnchor(hBox, 50d);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setWidth(1000);
        primaryStage.setHeight(500);
        primaryStage.centerOnScreen();
        primaryStage.setTitle("AudioClipDemo");
        primaryStage.show();

        play.setOnAction(event -> {
            if (playing) {
                mp.pause();
                pausing = true;
                playing = false;
                play.setText("播放");
            } else {
                if (pausing) {
                    mp.play();
                    playing = true;
                    pausing = false;
                    play.setText("暂停");
                } else {
                    initMusic(index);
                }
            }
        });

        stop.setOnAction(event -> {
            if (mp != null) {
                mp.dispose();
                process.setValue(0);
            }
        });

        pre.setOnAction(event -> {
            if (index <= 0) {
                index = list.size() - 1;
            } else {
                index--;
            }
            initMusic(index);
        });

        next.setOnAction(event -> {
            if (index >= list.size() - 1) {
                index = 0;
            } else {
                index++;
            }
            initMusic(index);
        });

        process.setOnMousePressed(event -> {
            if (mp != null) {
                mouse = true;
                mp.pause();
            }
        });

        process.setOnMouseReleased(event -> {
            if (mp != null) {
                mp.seek(Duration.seconds(process.getValue()));
                mouse = false;
                mp.play();
            }
        });

        // 音频频谱（波浪）
        /*mp.setAudioSpectrumListener(new AudioSpectrumListener() {
            @Override
            public void spectrumDataUpdate(double timestamp, double duration, float[] magnitudes, float[] phases) {

            }
        });*/
    }

    public void initMusic(int i) {
        if (mp != null) {
            mp.dispose();
        }
        mp = new MediaPlayer(list.get(i));

        mp.setOnReady(() -> {
            mp.volumeProperty().bind(sound.valueProperty());
            mp.play();

            process.setValue(0);
            process.setMin(0);
            process.setMax(mp.getTotalDuration().toSeconds());

            mp.currentTimeProperty().addListener((observable, oldValue, newValue) -> {
                if (!mouse) {
                    process.setValue(newValue.toSeconds());
                }
            });

            playing = true;
            pausing = false;
            play.setText("暂停");
        });
    }
}
